<template>
  <div id="templ">
      <div id="postcomment">
          <h3>提交评论</h3>
          <p class="p"></p>
          <!--双向数据绑定,获取content-->
          <textarea placeholder="亲,请输入你要评论的信息!" v-model="postcontent"  ></textarea>
          <!--按钮mint-ui的按钮-->
          <mt-button type="primary" size="large" @click="postcomment" :disabled="postcontent==''" >发表</mt-button>
      </div>
      <!--2.0获取评论信息列表-->
      <div id="list">
          <h3></h3>
          <p class="p"></p>
          <!--使用mui 的tableview-->
          <div v-for="(item,index) in list" :key="index">
              <div class="title">
                 <span>第{{index+1}}楼</span>
                  <span>用户:{{item.user_name}}</span>
                  <span>发表时间:{{item.add_time | datefmt("YYYY-MM-DD HH:mm:ss")}}</span>
              </div>
              <ul class="mui-table-view">
                  <li class="mui-table-view-cell" v-text="item.content"></li>
              </ul>
          </div>
      </div>
      <!--获取加载更多按钮-->
      <mt-button type="danger" size="large" plain  @click="getmoredata">加载更多数据</mt-button>
  </div>
</template>
<script>
  import common  from '../../kits/common';
  import { Toast } from 'mint-ui';
    export default {
        data(){
           return{
               postcontent:'',
               pageindex:1,
               list:[]
           }
        },
        props:['id'],
        created(){
            this.getcommentlist(this.pageindex);
        },
        methods:{
           //评论信息提交
            postcomment(){
                if(this.postcontent.trim().length<=0){
                    Toast("输入的评论信息不能为空!");
                    return;
                }
                var url=common.apidomain+"/api/postcomment/"+this.id;
                this.$http.post(url,{content:this.postcontent},{emulateJSON:true}).then(function (response) {
                    var body =response.body;
                    Toast(body.message);
                    this.list =[{
                        "user_name": "匿名用户",
                        "add_time": new Date(),
                        "content": this.postcontent
                    }].concat(this.list);
                    this.postcontent='';
                })
            },
            //获取评论信息的列表
            getcommentlist(pageindex){
                //进行判断pageindex
                pageindex =pageindex ||1;
                var url = common.apidomain+"/api/getcomments/"+this.id+"?pageindex="+pageindex;
                this.$http.get(url).then(function (response) {
                    var body =response.body;
                    if(body.status!=0){
                        Toast(body.message);
                        return;
                    }
                   // if(pageindex==1){
                    //    this.list=body.message;
                  //  }else{
                        this.list =this.list.concat(body.message);
                //    }
                })
            },
            //点击获取更多
            getmoredata(){
                this.pageindex++;
                this.getcommentlist(this.pageindex);
            }
        }
    }
</script>
<style>
    /*1.0实现提交评论样式*/
    #postcomment{
        padding: 5px;
    }
     .p{
        width: 100%;
        height: 1px;
        border-bottom: 1px solid rgba(0,0,0,0.3);
    }
   #list{
       padding:5px;
   }
    .title{
        padding: 5px;
        color: #6d6d72;
        font-size: 15px;
        background-color: rgba(0,0,0,0.1);
    }

</style>